var uid, gid;
var shares=null,bhref=false;
var Intent=null,File=null,Uri=null,main=null;
if(window.plus){
	plusReady();
}else{
	document.addEventListener("plusready",plusReady,false);
}
// H5 plus事件处理
function plusReady(){
	uid = plus.storage.getItem('uid');
	gid = plus.storage.getItem('gid');
	updateSerivces();
	if(plus.os.name=="Android"){
		Intent = plus.android.importClass("android.content.Intent");
		File = plus.android.importClass("java.io.File");
		Uri = plus.android.importClass("android.net.Uri");
		main = plus.android.runtimeMainActivity();
	}else{
		$('img').attr('src', '../images/ios.png');
	}
	ws = plus.webview.currentWebview();
	$('.qq-div').on('tap',function(){
		if(!uid){
			plus.nativeUI.toast('你还未登录请先登录');return;
		}
		switch($(this).index()){
			case 1:	//分享到QQ
			shareChange(0);
			break;
			case 2:	//分享到微信
			shareChange(1);
			break;
		}
	})
}
/**
 * 更新分享服务
 */
function updateSerivces(){
	plus.share.getServices( function(s){
		shares={};
		for(var i in s){
			var t=s[i];
			shares[t.id]=t;
		}
	}, function(e){
		plus.nativeUI.toast( "获取分享服务列表失败："+e.message );
	} );
}
/**
 * 弹出分享平台选择
 */
function shareChange(i){
	var ids=[{id:"qq"},{id:"weixin",ex:"WXSceneSession"}];
 	var s = shares[ids[i].id];
	if ( s.authenticated ) {
		shareMessage(shares[ids[i].id],ids[i].ex);
	} else {
		s.authorize( function(){
				shareMessage(shares[ids[i].id],ids[i].ex);
			},function(e){
			plus.nativeUI.alert( "认证授权失败"+e.message,null,'提示' );
		});
	}
}
	/**
   * 发送分享消息
   * @param {plus.share.ShareService} s
   */
function shareMessage(s,ex){
	var type = s.id == 'qq' ? '1' : '2';
	var str = "亲，您的好友向您推荐绥夏医生app";
	var msg={title : '绥夏医生' , extra : {scene:ex}  , content:str,href:apiRoot+'?m=home&c=index&a=shareapp&type='+type,thumbs:['/images/iconic.png']};
	s.send( msg, function(){
		plus.nativeUI.toast('分享成功');
	}, function(e){
//		console.log(JSON.stringify(e));
//		plus.nativeUI.toast( "转发失败" );
	});
}
